var express = require("express");
var router = express.Router();
var multer = require("multer");
var fs = require("fs");
var path = require("path");
var iconv = require("iconv-lite"); // 处理中文乱码
const OSS = require("ali-oss");
const ACCESSKEY_ID = "LTAI5tQicQW1p2tmDX4CHdV5";
const ACCESSKEY_ID_SECRET = "C2OdUA32cd7XTgnbAYjKizZk7Bd5PW";
const BUCKET_NAME = "ali-api";
const BUCKET_REGION = "oss-cn-hangzhou";
let store = null;
function createOssStore() {
  store = new OSS({
    region: BUCKET_REGION,
    accessKeyId: ACCESSKEY_ID,
    accessKeySecret: ACCESSKEY_ID_SECRET,
    bucket: BUCKET_NAME,
  });
//   store
//   .list()
//   .then(result => {
//     console.log('objects: %j', result.objects);

//   })
}
const storage = multer.diskStorage({
    destination: function (req, file, cb) {
      console.log("file", file);

      store.put(file.originalname, file).then(res => {
     console.log(res,222)

    })


    //   cb(null, path.join(__dirname, "../public/images")); // 存储位置
    },
    filename: function (req, file, cb) {
      const _time = Date.now().toString().slice(-4);
      cb(
        null,
        `-${_time}-${iconv.decode(file.originalname, "utf-8")}` // 存储文件名称处理
      );
    },
  });
router.post('/upload', multer({ dest: 'uploads/' }).single('file'),  function(req, res, next) {
    console.log('req.body', req.file)
    // createOssStore(); 
    store.put(req.file.originalname, req.file.path ).then(res => {
        console.log(res,222)
   
       })
  });
  
async function getBucketList(bucketName, region) {}
createOssStore()
module.exports = router;
